package com.example.towSpringBoot.service.impl;

import com.example.towSpringBoot.common.PageQuery;
import com.example.towSpringBoot.common.PageResult;
import com.example.towSpringBoot.dto.DepartmentDto;
import com.example.towSpringBoot.entity.Department;
import com.example.towSpringBoot.mapper.DepartmentMapper;
import com.example.towSpringBoot.service.DepartmentService;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xinTong
 * @since 2024-01-23
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
     * 查询部门信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult query(int pageNum, int pageSize) {
        // 先设置PageHelper必要参数
        PageHelper.startPage(pageNum, pageSize);
        // TODO: 差一个高级查询/
        Page<Department> page = departmentMapper.query();

        // 将Page数据赋值到PageResult中，展示给前端
        PageResult pageResult = new PageResult();
        pageResult.setList(page);
        pageResult.setTotal(page.getTotal());
        pageResult.setPageNum(page.getPageNum());
        pageResult.setPageSize(page.getPageSize());

        return pageResult;
    }

    /**
     * 删除部门
     * @param id
     */
    @Override
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    /**
     * 查询指定部门
     * @param id
     * @return
     */
    @Override
    public DepartmentDto info(Long id) {
        DepartmentDto dto = departmentMapper.info(id);
        return dto;
    }

    /**
     * 新增或修改
     * @param departmentDto
     */
    @Override
    public void saveOrUpdate(DepartmentDto departmentDto) {
        if (departmentDto.getId() != null){
            departmentMapper.update(departmentDto);
        }
        else{
            departmentMapper.save(departmentDto);
        }
    }

    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<Department> listAll() {
        List<Department> list =  departmentMapper.listAll();
        return list;
    }
}
